﻿using FySystem.Secure;
using System;
using System.Collections.Generic;
using System.Text;

namespace FySystem.Net.FyNetConnection.Src.Packing
{
    public class PackingWithFyModeV1
    {
        /// <summary>
        /// 按照第一个版本的的专用方式进行打包
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] PackData(byte[] bytes)
        {
            List<byte> res = new List<byte>();
            try
            {
                int length = bytes == null ? 0 : bytes.Length;
                byte[] b = BitConverter.GetBytes(length);
                ushort crc = 0;

                res.Add(0x20);
                res.Add(0x08);
                res.AddRange(b);            //长度

                if (bytes != null)
                {
                    res.AddRange(bytes);

                    crc = (ushort)CRC.CRC16(bytes, 0, bytes.Length);
                }

                b = BitConverter.GetBytes(crc);
                res.AddRange(b);
            }
            catch (Exception)
            {
                throw;
            }
            return res.ToArray();
        }
    }
}
